import { _decorator, Component, Node, Label, director } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../../../../../Script/Data/SSS_Consts";
import { ChengYu_GameData } from "../Data/ChengYu_GameData";
import { ChengYu_GameStorage } from "../Data/ChengYu_GameStorage";
import ChengYu_GamePanel_1 from "./ChengYu_GamePanel_1";
import SSS_GameEventManager, { SS_GameEventData } from 'db://assets/Script/Manager/SSS_GameEventManager';
import AdManager from 'db://assets/Script/Manager/AdManager';
import SSS_ShuaiFunction from 'db://assets/Script/Utils/SSS_ShuaiFunction';
import { SSS_Lockable } from 'db://assets/Script/Common/SSS_Lockable';
// /** GamePanel */

@ccclass('ChengYu_GamePanel')
export default class ChengYu_GamePanel extends Component {
    static Instance: ChengYu_GamePanel;
    @property(Node)
    bg_main: Node | null = null;
    //    // @property(Node)
    //    // bg_meihua: Node = null;
    @property(Node)
    Button_back: Node | null = null;
    //    // @property(Node)
    //    // Button_hongbao_ad: Node = null;
    @property(Node)
    Button_auto: Node | null = null;
    //    // @property(Node)
    //    // Button_strengthEvent: Node = null;
    //    // @property(Node)
    //    // Button_autoAnswerEvent: Node = null;
    @property(Label)
    Label_level: Label | null = null;
    @property(Label)
    Label_auto: Label | null = null;
    @property(Label)
    Label_autoNumber: Label | null = null;
    @property(Node)
    ad: Node | null = null;
    @property(Label)
    Label_time: Label | null = null;
    //    // @property(Node)
    //    // Grid_HongBao: Node = null;
    //    // @property(Button)
    //    // helpBtn: Button = null;
    timer;
    onLoad() {
        ChengYu_GamePanel.Instance = this;
        // this.ShowOn();

        console.log(`ChengYu_GamePanel.onLoad`);
    }
    ShowOn() {
        // let url = HttpManager.getInstance().getFullUrl(HallConst.HTTP_CONST.reward_sum_user);
        // let params2: HttpRequestParams = {
        //     url: url,
        //     requestType: HttpConst.REQUEST_TYPE.get,
        //     callBack: (code: number, res: any) => {
        //         // xwLog(" 返回 =>", res);
        //         console.log(`222`);

        //         // num: "8.54";
        //         // numRate: "854.000";
        //         // rate: "1";

        //         UserManger.getInstance().getUserInfo().setYuanBao(res.num);

        //         console.log(`元宝`, UserManger.getInstance().getUserInfo().getYuanBao());

        this.node.active = true;
        this.Init();
        this.AddListener();
        this.Refresh();

        this.schedule(this.TimerCallback, 1);

        // tween(ChengYu_GamePanel.Instance.Grid_HongBao)
        // .repeatForever(tween().to(0.05, { angle: 10 }).to(0.1, { angle: -10 }).to(0.1, { angle: 10 }).to(0.1, { angle: -10 }).to(0.05, { angle: 0 }).delay(2))
        // .start();
    }
    onDestroy() {
        console.log(`游戏界面消失---------`);
        // ADHelper.getInstance().hideBannerAd();
    }
    TimerCallback() {
        ChengYu_GameData.GameData.GamePlayTime++;
        this.RefreshTime();
    }
    Init() {
        // let mp = AdMpHelper.getInstance().getAdMpIdByAdMpName(AdMpName2AdMpNameInfAdMpCfg.levelBanner);
        // ADHelper.getInstance().showBannerAd(mp);

        // this.ui_middle.scale = visibleHeight / 1500;


        // //短屏幕手机适配
        // const visibleHeight = view.getVisibleSize().height;
        // if (visibleHeight < 1500) {
        //     ChengYu_GamePanel.Instance.bg_main.setScale(0.8);
        //     let widget = ChengYu_GamePanel.Instance.bg_main.getComponent(Widget);
        //     widget.top = 150;
        // }


        // GamePanel.Instance.Button_strengthEvent.active = false;
        // GamePanel.Instance.Button_autoAnswerEvent.active = false;

        // if (XXL_GameData.GameData.IsStrengthEventState === true) {
        //     GamePanel.Instance.Button_strengthEvent.active = true;
        // }

        // if (XXL_GameData.GameData.IsAutoAnswerEventState === true) {
        //     GamePanel.Instance.Button_autoAnswerEvent.active = true;
        // }
    }
    Refresh() {
        let level = ChengYu_GameData.Get_CurrentLevelIndex(); // UserManger.getInstance().getUserInfo().getCurLevel() + 1;
        this.Label_level.string = `第${level}关`;

        this.Refresh_tip();
        // this.Label_autoNumber.string = `${ChengYu_GameStorage.data.tip_number}`;
        // this.Label_autoNumber.string = `${UserManger.getInstance().getUserInfo().getAutoAnswerNum()}`;

        // this.Button_hongbao_ad.active = !XXL_GameData.GameData.IsHongBao1Get;
        // this.Button_strengthEvent.active = XXL_GameData.GameData.IsStrengthEventState;
        // this.Button_autoAnswerEvent.active = XXL_GameData.GameData.IsAutoAnswerEventState;

        // if (AppConfig.pureSwitch) {
        //     this.Button_hongbao_ad.active = false;
        // }

        this.RefreshTime();
    }
    RefreshTime() {
        const minute = (100 + Math.floor(ChengYu_GameData.GameData.GamePlayTime / 60)).toString().slice(1);
        const second = (100 + (ChengYu_GameData.GameData.GamePlayTime % 60)).toString().slice(1);
        this.Label_time.string = `${minute}:${second}`;
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    AddListener() {
        this.Button_back.on("click", this.On_Button_back, this);
        // this.Button_hongbao_ad.on("click", this.On_Button_hongbao_ad, this);
        // this.Button_strengthEvent.on("click", this.On_Button_strengthEvent, this);
        // this.Button_autoAnswerEvent.on("click", this.On_Button_autoAnswerEvent, this);

        this.Button_auto.on("click", this.On_Button_auto, this);

        SSS_GameEventManager.eventTarget.on("GamePanel_Refresh", this.Refresh, this);
    }
    RemoveListener() {
        this.Button_back.off("click", this.On_Button_back, this);
        // this.Button_hongbao_ad.off("click", this.On_Button_hongbao_ad, this);
        // this.Button_strengthEvent.off("click", this.On_Button_strengthEvent, this);
        // this.Button_autoAnswerEvent.off("click", this.On_Button_autoAnswerEvent, this);

        this.Button_auto.off("click", this.On_Button_auto, this);

        // SSS_GameEventManager.eventTarget.off("GamePanel_Refresh", this.Refresh, this);
    }
    On_Button_back() {
        director.loadScene(SSS_Consts.SceneName.MainScene);
        // SceneManager.getInstance().loadSceneWithBundle(BundleType.Hall, SceneConst.SCENE_NAME.main);
    }
    //    // On_Button_hongbao_ad() {
    //    //     const params = {
    //    //         adMpName2AdMpNameInfAdMpCfg: AdMpName2AdMpNameInfAdMpCfg.levelHongbao,
    //    //     };
    //    //     hallFunc.showPanel(HallResConstant.PREFABS_PATH.HongBaoOpen_QiYuPanel, params);
    //    // }
    On_Button_strengthEvent() {
        if (ChengYu_GameData.GameData.IsGameEnd) {
            return;
        }

        // hallFunc.showPanel(HallResConstant.PREFABS_PATH.StrengthRecover, 1);
    }
    //    // On_Button_autoAnswerEvent() {
    //    //     // if (XXL_GameData.GameData.IsGameEnd) {
    //    //     //     return;
    //    //     // }
    //    //     gameFunc.showPanel(GameResConst.PREFABS_PATH.AutoAnswer, 1);
    //    // }
    Refresh_tip() {
        this.Label_autoNumber.string = `${ChengYu_GameStorage.data.tip_number}`;
        this.ad.active = ChengYu_GameStorage.data.tip_number <= 0;
    }

    增加自动过关次数() {
        ChengYu_GameStorage.data.tip_number += ChengYu_GameData.add_tip_number;
        ChengYu_GameStorage.saveData();
        this.Refresh_tip();
    }

    @SSS_Lockable(1000)
    On_Button_auto() {
        console.log(`自动答题`);

        if (ChengYu_GameData.GameData.IsGameEnd) {
            return;
        }


        if (ChengYu_GameStorage.data.tip_number > 0) {
            ChengYu_GameStorage.data.tip_number--;
            ChengYu_GameStorage.saveData();
            this.Refresh_tip();

            ChengYu_GamePanel_1.Instance.AutoAnswer();
        }
        else {
            SSS_ShuaiFunction.AD_callback(() => {
                this.增加自动过关次数();
            });


            // AdManager.是否看完广告 = false;
            // AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         this.增加自动过关次数();
            //     },
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         console.log("复活失败！");
            //     },
            // );
        }
    }
}

// /** 调试 */
window["ChengYu_GamePanel"] = ChengYu_GamePanel;